package com.atfusu.controller;

import com.atfusu.bean.entity.HouseUser;
import com.atfusu.service.HouseUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * Title:
 * Author: fusu
 * DateTime:  2022/11/30
 * Description:
 **/
@Controller
@RequestMapping("/houseUser")
public class HouseUserController {

    private static final String PAGE_CREATE = "houseUser/create";
    private static final String PAGE_EDIT = "houseUser/edit";
    private static final String PAGE_SUCCESS = "common/success";

    @DubboReference
    private HouseUserService houseUserService;

    @RequestMapping("/create/{houseId}")
    public String create(@PathVariable Long houseId, Map map) {

        map.put("houseId", houseId);

        return PAGE_CREATE;
    }

    @RequestMapping("/save")
    public String save(HouseUser houseUser) {
        houseUserService.insert(houseUser);

        return PAGE_SUCCESS;
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Map map) {
        HouseUser houseUser = houseUserService.getById(id);
        map.put("houseUser", houseUser);
        return PAGE_EDIT;
    }

    @RequestMapping("/update")
    public String update(HouseUser houseUser) {
        houseUserService.update(houseUser);
        return PAGE_SUCCESS;
    }

    @RequestMapping("/delete/{houseId}/{id}")
    public String delete(@PathVariable Long houseId, @PathVariable Long id) {
        //根据id删除数据，根据houseId页面跳转
        houseUserService.delete(id);

        return "redirect:/house/show/" + houseId;
    }
}
